#include "../../include/IdRegister.h"

/**
 * 构造函数
 */
IdRegister::IdRegister(const string &_front,const int _length):front(_front),length(_length) {};
/**
 * 序列化函数
 */
string IdRegister::toString() {
    string str = front + " " + to_string(length);
    str += " " + to_string(idSet.size());
    set<string>::iterator iter;
    for(iter = idSet.begin();iter != idSet.end();iter++) {
        str += " " + *iter;
    }
    return str;
}

/**
 * 反序列化函数
 */
IdRegister* IdRegister::strToIdRegister(istringstream &iss) {
    char split = ' ';       // 分隔符
    string token;		// 接收缓冲区
    string front;
    int length,count;

    // 接收front
    getline(iss, front, split);
    // 接收length
    getline(iss, token, split);
    length = stoi(token);

    IdRegister* idReg = new IdRegister(front,length);

    // 接收count;
    getline(iss, token, split);
    count = stoi(token);

    for(int i=0;i<count;i++) {
        getline(iss, token, split);
        idReg->idSet.insert(token);
    }
    return idReg;
}